<template>
  <Dialog :title="!formType ? '导入平台会员' : '导入会员关系'" v-model="dialogVisible">
    <el-form
      v-if="formType == 0"
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="wid" prop="wids">
        <el-input v-model="formData.wids" placeholder="wid列表半角,逗号分开" />
      </el-form-item>
    </el-form>

    <el-upload
      v-if="formType == 1"
      :auto-upload="false"
      v-model="multiFile"
      with-credentials
      class="upload-demo"
      :limit="1"
      v-model:file-list="multiFile"
      drag
    >
      <el-icon class="el-icon--upload"><upload-filled /></el-icon>
      <div class="el-upload__text"> Drop file here or <em>click to upload</em> </div>
      <template #tip>
        <div class="el-upload__tip"> jpg/png files with a size less than 500kb </div>
      </template>
    </el-upload>

    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { MemberApi, MemberVO } from '@/api/wo/member'
import { UploadFilled } from '@element-plus/icons-vue'
import {al} from "../../../../../dist/assets/index-y4H15elt";

/** 平台会员 表单 */
defineOptions({ name: 'MemberForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref(0) // 表单的类型：create - 新增；update - 修改
const formData = ref({
  wids: undefined
})
const formRules = reactive({
})
const formRef = ref() // 表单 Ref

const multiFile = ref()

/** 子表的表单 */
const subTabsName = ref('memberTeam')
const memberTeamFormRef = ref()
const accountFormRef = ref()

/** 打开弹窗 */
const open = async (type: number, widList: Array<number>, id?: number) => {
  dialogVisible.value = true
  formType.value = type
  // dialogTitle.value = t('action.' + type)
  // formType.value = type
  resetForm()
  if(widList.length>1){
    formData.value.wids = widList.join(',') as any
  }else {
    formData.value.wids = null as any
  }

  // 修改时，设置数据
  // if (id) {
  //   formLoading.value = true
  //   try {
  //     formData.value = await MemberApi.getMember(id)
  //   } finally {
  //     formLoading.value = false
  //   }
  // }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  if (formType.value == 1) {
    // 提交请求
    formLoading.value = true
    try {
      await MemberApi.importTeam(multiFile.value[0].raw)
      message.success('导入成功')
      dialogVisible.value = false
      // 发送操作成功的事件
      emit('success')
    } finally {
      formLoading.value = false
    }
  }

  // 校验表单
  await formRef.value.validate()

  // 提交请求
  formLoading.value = true
  try {
    await MemberApi.importMember(formData.value.wids?.split(','))
    message.success('导入成功')
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    wids: undefined
  }
  formRef.value?.resetFields()
}
</script>
